﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gfw.BLL.Basic.Site;
using Gfw.Repositories.Site;
using Gfw.Web.Areas.Admin.Models;
using Gfw.Web.Common;
using Gfw.Web.Models.Site;

namespace Gfw.Web.Areas.Admin.Controllers
{
    [LAuthorize(RequireRole = "Admin")]
    public class AnnouncementController : Controller
    {
        /// <summary>
        /// 当前选择的页数
        /// </summary>
        /// 如果是选择参数，也可以这样处理
        int PageIndex
        {
            get { return Session["AnnouncementController_PageIndex"].ConvertTo<int>(1); }
            set { Session["AnnouncementController_PageIndex"] = value; }
        }

        private AnnouncementQueryModel QueryModel
        {
            get { return Session["AnnouncementController_AnnouncementQueryModel"].ConvertTo<AnnouncementQueryModel>(null); }
            set { Session["AnnouncementController_AnnouncementQueryModel"] = value; }
        }

        //
        // GET: /Admin/Announcement/

        public ActionResult Index(AnnouncementQueryModel model)
        {
            QueryModel = model;
            PageIndex = 1;
            return View(model);

        }

        #region add by lif 分页和查询等

        /// <summary>
        /// 返回指定页面
        /// </summary>
        /// <param name="page">重第一页开始计算</param>
        /// <returns></returns>
        public ActionResult List(int? page)
        {
            int totalSize;
            int index = page ?? PageIndex;
            if (index < 1) index = 1;

            var list = AnnouncementLogic.Query(false, QueryModel.Key, QueryModel.Start, QueryModel.End, index - 1, ViewHelper.PageSize, out totalSize);
            ViewBag.TotalSize = totalSize;
            PageIndex = ViewBag.PageIndex = index;

            return PartialView(list);

        }


        #endregion

        //
        // GET: /Admin/Announcement/Detail
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Detail(string id)
        {
            AnnouncementModel model = AnnouncementLogic.GetAnnouncementByID(id);
            if (model == null)
                return HttpNotFound();

            return View(model);
        }



        //
        // GET: /Admin/Announcement/Create
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/Announcement/Create
        /// <summary>
        /// 
        /// </summary>
        /// <param name="anct"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(AnnouncementModel anct, FormCollection collection)
        {
            try
            {
                if (string.IsNullOrEmpty(anct.AnnouncementKeyWord))
                    anct.AnnouncementKeyWord = "";

                anct.AnnouncementContent = collection["editor1"];
                AnnouncementLogic.CreateAnnouncement(anct);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Announcement/Edit/5
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            AnnouncementModel model = AnnouncementLogic.GetAnnouncementByID(id);
            if (model == null)
                return HttpNotFound();

            return View(model);
        }

        //
        // POST: /Admin/Announcement/Edit/5
        /// <summary>
        /// 
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(AnnouncementModel anct, FormCollection collection)
        {
            try
            {
                anct.AnnouncementContent = collection["editor1"];
                AnnouncementLogic.EditAnnouncement(anct);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /Admin/Announcement/Delete/5
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        [HttpPost]
        public void Delete(string id)
        {
            try
            {
                if (AnnouncementLogic.GetAnnouncementByID(id) == null)
                    return;

                AnnouncementLogic.DeleteAnnouncement(id);
                return;
            }
            catch
            {
                return;
            }
        }
    }
}
